﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CardLogistics.Web.Components;
using System.Net;
using System.IO;

namespace CardLogistics.Web.MainPage.CardRecovery.ChangeBadCard
{
    public partial class ChangeBadCardDetailedRpt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string ddlSUPPName = Request.QueryString["ddlSUPPName"];//供应商
            string ddlSalesName = Request.QueryString["ddlSalesName"];//销售网点
            string StartCardNo = Request.QueryString["StartCardNo"];//开始系统卡号
            string EndCardNo = Request.QueryString["EndCardNo"];//结束系统卡号
            string startDate = Request.QueryString["startDate"];//开始售卡日期
            string endDate = Request.QueryString["endDate"];//结束售卡日期

            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" SELECT * FROM v_jy_bicardacctb_gys_sale WHERE 1=1");
            if (ddlSUPPName.Trim().Length > 0 && ddlSUPPName.Trim()!="0")
                sbStr.Append(" and suppliersid = '" + ddlSUPPName.Trim() + "'");//供应商
            if (ddlSalesName.Trim().Length > 0 && ddlSalesName.Trim() != "0")
                sbStr.Append(" and salesId = '" + ddlSalesName.Trim() + "'");//销售网点
            if (StartCardNo.Trim().Length > 0 && StartCardNo.Trim() != "undefined")
                sbStr.Append(" and CardNo >=" + StartCardNo.Trim());//开始系统卡号
            if (EndCardNo.Trim().Length > 0 && EndCardNo.Trim() != "undefined")
                sbStr.Append(" and CardNo <=" + EndCardNo.Trim());//结束系统卡号
            if (startDate.Trim().Length > 0 && startDate.Trim() != "undefined")
                sbStr.Append(" and CRDSLDATE>='" +startDate.Replace("-","").Trim() + "'");//开始售卡日期
            if (endDate.Trim().Length > 0 && endDate.Trim() != "undefined")
                sbStr.Append(" and CRDSLDATE<='" + endDate.Replace("-", "").Trim() + "'");//结束售卡日期

            sbStr.Append(" order by CrdpAccNo ");


            #region
            // webapi路径：http://10.0.8.128:8080/api/logistics/GetCardInfo?bgTime=20160101&endTime=20160101




            //string bgTime = "20160101";
            //string endTime = "20160101";

            //string jsonParas = "{\"bgTime\":\"" + bgTime + "\",\"endTime\":\"" + endTime + "\"}";
            //string strRetVal = GetResponseData("", "http://10.0.8.128:8080/api/logistics/GetCardInfo?bgTime=20160101&endTime=20160101");

            #endregion


            OracleXMLReportData.GenNodeXmlData(this, sbStr.ToString(), true);
        }

        /// <summary>  
        /// 返回JSon数据  
        /// </summary>  
        /// <param name="JSONData">要处理的JSON数据</param>  
        /// <param name="Url">要提交的URL</param>  
        /// <returns>返回的JSON处理字符串</returns>  
        public string GetResponseData(string JSONData, string Url)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType = "text/xml";
            Stream reqstream = request.GetRequestStream();
            reqstream.Write(bytes, 0, bytes.Length);

            //声明一个HttpWebRequest请求  
            request.Timeout = 90000;
            //设置连接超时时间  
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;

            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string strResult = streamReader.ReadToEnd();
            streamReceive.Dispose();
            streamReader.Dispose();

            return strResult;
        }
    }
}